package com.mvp.lt.airlineview.data.source.helper;

import com.mvp.lt.airlineview.data.source.db.DaoMaster;
import com.mvp.lt.airlineview.data.source.db.DaoSession;
import com.mvp.lt.airlineview.data.source.db.UserDao;
import com.mvp.lt.airlineview.App;

/**
 * $activityName
 * 数据库管理类
 *
 * @author LiuTao
 * @date 2019/2/20/020
 */


public class DBManager {

    public static String dbName = "paiwujie.db";
    private static DBManager mDBManager;
    private final DaoSession mDaoWriteSession;
    private final DaoSession mDaoReadSession;

    /**
     * 获取单例
     *
     * @return
     */
    public static DBManager getInstance() {
        if (mDBManager == null) {
            synchronized (DBManager.class) {
                if (mDBManager == null) {
                    mDBManager = new DBManager();
                }
            }
        }
        return mDBManager;
    }

    private DBManager() {
        PwjOpenHelper helper = new PwjOpenHelper(App.getContext(), dbName, null);
        mDaoWriteSession = new DaoMaster(helper.getWritableDatabase()).newSession();
        mDaoReadSession = new DaoMaster(helper.getReadableDatabase()).newSession();
        // mDaoWriteSession = new DaoMaster(helper.getWritableDatabase()).newSession();
        //  DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(BaseApp.getContext(), dbName, null);
    }

    /**
     * 增删改 可写
     * 查 可读
     */
    private static class Holder {

    }

    //-----------------创建各种Dao-------------------
    public UserDao getWriteUserDao() {
        return mDaoWriteSession.getUserDao();
    }

    public UserDao getReadUserDao() {
        return mDaoReadSession.getUserDao();
    }
}
